package constant.milk.cashbook.budgetstats;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.StatsData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.select.SelectMainActivity;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;

public class StatsListActivity extends SuperActivity
{
	public static final String YEAR1_DATA = "YEAR1_DATA";
	public static final String MONTH1_DATA = "MONTH1_DATA";
	public static final String DAY1_DATA = "DAY1_DATA";
	public static final String YEAR2_DATA = "YEAR2_DATA";
	public static final String MONTH2_DATA = "MONTH2_DATA";
	public static final String DAY2_DATA = "DAY2_DATA";
	
	private TextView firstDate;
	private TextView lastDate;
	
	private ListView listView;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private StatsAdapter adapter;
	
	private ArrayList<StatsData> resultList;
	
	private DBSelectQuery select;
	
	private int type;
	private int flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.stats_list_activity);
		
		setTitle("통계목록");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		firstDate = (TextView) findViewById(R.id.statsListFirstDateTextView);
		lastDate = (TextView) findViewById(R.id.statsListLastDateTextView);
		
		listView = (ListView) findViewById(R.id.listView);
	}

	@Override
	protected void setOnClick()
	{
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		Calendar cal = Calendar.getInstance();
		
		Intent intent = getIntent();
		
		type = intent.getIntExtra(BudgetStatsActivity.TYPE, 0);
		flag = intent.getIntExtra(StatsSearchActivity.FLAG, 0);
		
		year1 = intent.getIntExtra(SelectMainActivity.YEAR1_DATA, cal.get(Calendar.YEAR));
		month1 = intent.getIntExtra(SelectMainActivity.MONTH1_DATA, cal.get(Calendar.MONTH));
		day1 = intent.getIntExtra(SelectMainActivity.DAY1_DATA, cal.get(Calendar.DAY_OF_MONTH));
		year2 = intent.getIntExtra(SelectMainActivity.YEAR2_DATA, cal.get(Calendar.YEAR));
		month2 = intent.getIntExtra(SelectMainActivity.MONTH2_DATA, cal.get(Calendar.MONTH));
		day2 = intent.getIntExtra(SelectMainActivity.DAY2_DATA, cal.get(Calendar.DAY_OF_MONTH));
		
		firstDate.setText(MilkUtil.getDate(year1, month1+1, day1));
		lastDate.setText(MilkUtil.getDate(year2, month2+1, day2));
		
		setStatsData();
		
		listView.setOnItemClickListener(onItemClickListener);
		
		adapter = new StatsAdapter(
				superContext, R.layout.stats_list_item, resultList);
		listView.setAdapter(adapter);
	}
	
	private void setStatsData()
	{
		resultList = new ArrayList<StatsData>();
		
		ArrayList<StatsData> list = select.getInOutStatsList(type, flag,
				MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
		
		ArrayList<StatsData> changeList = null;
		
		if (type == 0)
		{
			changeList = select.getInOutStatsList(-1, flag, 
					MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
		}
		
		BigDecimal total = BigDecimal.ZERO;
		
		ArrayList<StatsData> tempList = new ArrayList<StatsData>();
		
		for (int i = 0; i < list.size(); i++)
		{
			total = total.add(new BigDecimal(list.get(i).getMoney()));
			
			if (type == 0 && changeList != null)
			{
				for (int j = 0; j < changeList.size(); j++)
				{
					if (list.get(i).getTitle().equals(changeList.get(j).getTitle()))
					{
						total = total.subtract(new BigDecimal(changeList.get(j).getMoney()));
					}
				}
			}
			
			StatsData data = new StatsData();
			data.setMoney(total.toString());
			data.setTitle(list.get(i).getTitle());
			
			tempList.add(data);
		}
		
		for (int i = 0; i < tempList.size(); i++)
		{
			StatsData statsData = new StatsData();
			StatsData data = tempList.get(i);
			
			if (data.getMoney().equals("0"))
			{
				continue;
			}
			
			BigDecimal percent = BigDecimal.ZERO;
			BigDecimal money = new BigDecimal(data.getMoney());
			percent = money.divide(total, 4, BigDecimal.ROUND_UP);
			percent = percent.multiply(new BigDecimal("100"));
			
			statsData.setMoney(money.toString());
			statsData.setPercent(percent.doubleValue() + "");
			statsData.setTitle(data.getTitle());
			
			resultList.add(statsData);
		}
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
		}
	};
	
	private class StatsAdapter extends ArrayAdapter<StatsData>
	{
		public StatsAdapter(Context context, int textViewResourceId, ArrayList<StatsData> objects) 
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.stats_list_item, null);
			}
			
			StatsData data = getItem(position);
			
			MilkTextView title = (MilkTextView) v.findViewById(R.id.statsItemTitleTextView);
			MilkTextView money = (MilkTextView) v.findViewById(R.id.statsItemMoneyTextView);
			
			title.setTextSize(TEXT_SIZE);
			money.setTextSize(TEXT_SIZE);
			
			title.setText(data.getTitle()  + " (" + data.getPercent() + " %)");
			money.setText(MilkUtil.getWon(data.getMoney()));
			
			return v;
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_STATS_LIST);
		startActivity(intent);
	}

}
